//
//  JGTabBar.m
//  JGQQZoneHD
//
//  Created by JJetGu on 15/10/7.
//  Copyright (c) 2015年 JJetGu. All rights reserved.
//

#import "JGTabBar.h"
#import "UIView+Extension.h"
#import "JGConst.h"
#import "JGTabBarButton.h"

@interface JGTabBar ()

@property (nonatomic, weak) JGTabBarButton *selelctedButton;

@end

@implementation JGTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 创建按钮
        JGTabBarButton *firstButton = [self setupButton:@"tab_bar_feed_icon" title:@"全部动态"];
        firstButton.enabled = NO;
        self.selelctedButton = firstButton;
        
        [self setupButton:@"tab_bar_passive_feed_icon" title:@"与我相关"];
        [self setupButton:@"tab_bar_pic_wall_icon" title:@"照片墙"];
        [self setupButton:@"tab_bar_e_album_icon" title:@"电子相框"];
        [self setupButton:@"tab_bar_friend_icon" title:@"好友"];
        [self setupButton:@"tab_bar_e_more_icon" title:@"更多"];
        
    }
    
    return self;
    
}

- (JGTabBarButton *)setupButton:(NSString *)icon title:(NSString *)title
{
    JGTabBarButton *button = [[JGTabBarButton alloc] init];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:button];
    return button;
}

- (void)buttonClick:(JGTabBarButton *)button
{
    self.selelctedButton.enabled = YES;
    button.enabled = NO; // button进入highlighted\disabled,图标默认会变灰
    self.selelctedButton = button;
    
    // 发出通知
    [[NSNotificationCenter defaultCenter] postNotificationName:JGTabBarDidSelectNotification object:nil userInfo:@{JGTabBarSelectIndex : @(button.tag)}];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    int count = self.subviews.count;
    for (int i = 0; i < count; i++) {
        JGTabBarButton *button = self.subviews[i];
        button.height = self.height / count;
        button.width = self.width;
        button.x = 0;
        button.y = button.height * i;
        button.tag = i;
    }
    
}






@end
